export const sned = ({
	commit
}, p) => {
	let websocket_tag = 0;
	var readyState = new Array("正在连接", "已建立连接", "正在关闭连接", "已关闭连接");
	if(!window.socket) {
		try {
			var host = window.config.socket_host;
			console.log("订阅地址" + host)
			window.socket = new Reconnecting(host);
			window.socket.onopen = function() {
				console.log(readyState[window.socket.readyState]);
				if(readyState[window.socket.readyState] == "已建立连接") {
					for(var i of p) {
						window.socket.send(JSON.stringify(i));
					}
					console.log("已经完成" + JSON.stringify(p) + "的订阅");
				}
			}
			window.socket.onmessage = function(event) {
				var data = JSON.parse(event.data)
				if(data.data) {
//					console.log(data);
					if(data.channel.indexOf('multi_platform') == 0) {
						commit('multi_platform', data)
					}else{
						switch (data.channel){
							case 'rise_and_fall':
								commit('rise_and_fall', data.data)
								break;
							case 'spot_latest_volume_price':
								commit('count_board', data.data)
								break;
							default:
								break;
						}
					}
				}
			}
			window.socket.onclose = function() {
				console.log("关闭信息： " + readyState[socket.readyState] + "如果收到这条消息，说明服务器已经对应关闭这是一条来自服务的回调信息");
			}
		} catch(exception) {
			console.log("有错误发生");
		}
	} else {
		console.log(JSON.stringify(p));
		for(var i of p) {
			if(readyState[window.socket.readyState] == "已建立连接") {
				for(var i of p) {
					window.socket.send(JSON.stringify(i));
					console.log("已经完成" + JSON.stringify(p) + "的订阅");
				}
			}
		}
	}
	setInterval(function() {
		websocket_tag++;
		if(websocket_tag >= 30 && readyState[window.socket.readyState] == "已建立连接") {
			window.socket.send("1");
			websocket_tag = 0;
		}
	}, 1000)
}